from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse, render, redirect, reverse
from django.conf import settings
from .models import *


class LoginAuth(MiddlewareMixin):
    def process_request(self, request):
        path = request.path
        if path in settings.WHITE_MENU:
            return None
        username = request.session.get('username', '')

        if username:
            request.account = UserProfile.objects.filter(pk=request.session.get("id", "")).first()
            print("login-user==", request.account)
        else:
            return redirect(reverse('login'))


class AccountResponse(MiddlewareMixin):
    def process_template_response(self, request, response):
        response.context_data.update({"user": request.account})
        print("user=12==", request.account)
        return response
